﻿package classes.store.storeclasses
{ 
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.filters.GlowFilter; 
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	 
	import classes.GameSetting;
	
	import flash.events.Event;
	import flash.events.MouseEvent; 
	
	import com.mttang.event.CommonEvent;
	import com.mttang.event.JEvent;
	 
	/**
	 * 商品列表类
	 * @author rock 
	 */
	public class ShopList extends Sprite 
	{  
		//保存当前选中的物品
		public static var selectGodds:Object;
	     
		public var shopJsonArr:Array = []; 
		private var _mGoodslistCon:MovieClip;//包含所有商品的容器 
		private var _nGoodslistConX:Number = 235;
		private var _nGoodslistConY:Number = 50;
		private var _nGoodsX:int;//物品显示X位置
		private var _nGoodsY:int;//物品显示Y位置 
		private var _nGoodsIndeX:int = 0;//物品X轴索引
		private var _nGoodsIndeY:int = 0;//物品Y轴索引
		
		//加点击效果
		private var _gGlowFtr:GlowFilter;
		private var _aFtrArr:Array = [];
		private var _mGoodCkMc:MovieClip;//记载上一次点击过的商品
		
		private var  scrollCls:ScrollCls;
		
		public function ShopList(_shopJsonArr:Array):void
		{ 
		    this.shopJsonArr=_shopJsonArr;
 		} 
		
		//开始加载并显示商品
		public function initData():void
		{
			//生成滤镜
			creatFilter(); 
			//实例化商店物品列表容器
		 	_mGoodslistCon = new MovieClip(); 
			addChild(_mGoodslistCon);
			 
			
			//传递参数
			for (var i:int = 0; i < shopJsonArr.length; i++)
			{ 
				if (i >1)
				{    
				   if (i % 2 == 0)
				   {   
					   _nGoodsIndeX = 0; 
					   _nGoodsIndeY++; 
					   _nGoodsY = _nGoodsIndeY *60;//Y中间间隔10
				   } 
				   _nGoodsX = _nGoodsIndeX * 130+10;//X中间间隔10
				   _nGoodsIndeX++;
				}
				else { 
				   _nGoodsX = i * 130+10;//X中间间隔10
				   _nGoodsY = 0;
				} 
				setData(shopJsonArr[i].name,shopJsonArr[i].petid,shopJsonArr[i].price,shopJsonArr[i].level,shopJsonArr[i].status,shopJsonArr[i].anti,shopJsonArr[i].skills,_nGoodsX,_nGoodsY);
			} 
			//生成 滚动条
			scrollCls=new ScrollCls();
			addChild(scrollCls); 
			scrollCls.x = 292;
			scrollCls.y = 85;
			showScroll(_mGoodslistCon,275,300);//显示滚动条
		}
		//物品类名，类型，名称，ID，宠物币价格，数量，物品X，物品Y
		public function setData(_name:String,_petid:int,_price:int,_level:int,_status:int,_anti:int,_skills:String,_goodsX:int,_goodsY:int):void
		{    
			try
			{
			var _mBigGoodsMc:MovieClip = new StoreGoodsImg();//商品抽屉
			var _mImgMc:MovieClip = new (GameSetting.petRsc.contentLoaderInfo.applicationDomain.getDefinition(_name+""+_petid) as Class)();//商品图片
			//var _mImgMc:MovieClip=new (getDefinitionByName(_name+""+_petid) as Class)();
			_mImgMc.x = 5+_mImgMc.width/2;
			_mImgMc.y = 5 + _mImgMc.height / 2;  
			_mImgMc.scaleX *= .9;
			_mImgMc.scaleY *= .9;
			
			_mBigGoodsMc.addChild(_mImgMc); 
			_mBigGoodsMc.mouseChildren = false;
			_mGoodslistCon.addChild(_mBigGoodsMc);
			
			//赋值 
			_mBigGoodsMc.name = _name;
			_mBigGoodsMc.petid = _petid;
			_mBigGoodsMc.price = _price;  
			_mBigGoodsMc.level = _level;
			_mBigGoodsMc.status = _status;
			_mBigGoodsMc.anti = _anti;
			_mBigGoodsMc.skills = _skills; 
			
			_mBigGoodsMc.x = _goodsX;
			_mBigGoodsMc.y = _goodsY; 
			_mBigGoodsMc.money.text = _price;
			_mGoodslistCon.addEventListener(MouseEvent.CLICK, glCon_CkHandler); 
			}catch (e:*)
			{
			    trace(e.type);	
			}
		}
		private function glCon_CkHandler(e:MouseEvent):void
		{ 
			//删除上一个物品的滤镜效果  
			_aFtrArr = [];
			if(_mGoodCkMc)_mGoodCkMc.filters = _aFtrArr;//说明：初次点击时_mGoodCkMc不存在
			
			//添加当前物品的滤镜效果
			_aFtrArr.push(_gGlowFtr); 
			_mGoodCkMc = e.target as MovieClip; 
			_mGoodCkMc.filters = _aFtrArr;
			
			//保存当前选中的商品
			selectGodds = new Object(); 
			selectGodds.name = e.target.name;
			selectGodds.petid = e.target.petid;
			selectGodds.price = e.target.price;
			selectGodds.level = e.target.level;
			selectGodds.status = e.target.status; 
			selectGodds.anti = e.target.anti;
			selectGodds.skills = e.target.skills;  
			
			dispatchEvent(new JEvent(CommonEvent.GOODS_CLICK)); 
		}
		
		private function creatFilter():void
		{
		   	_gGlowFtr = new GlowFilter(0x65FFCC,1,15,15);
		} 
		
		//删除背包容器中的所有物品 
		public function cleanData():void
		{  
			var i:Number = 0;
			    while(i<_mGoodslistCon.numChildren)
			    {  
			       _mGoodslistCon.removeChild(_mGoodslistCon.getChildAt(i));  
			    }
			    i++; 
				_nGoodsX = 0;
				_nGoodsY = 0;
				_nGoodsIndeX = 0;
				_nGoodsIndeY = 0;
			
			/*
			selectGodds.type = "";
			selectGodds.name = "";
			selectGodds.id = "";
			selectGodds.className = "";
			selectGodds.petMoney = "";
			selectGodds.money = "";
			selectGodds.quantity = "";
			selectGodds.hasNum = "";*/
		}
		
		//生成滚动条
		private function showScroll(_displayObj:DisplayObject,_objWidht:Number,_objHeight:Number):void
		{    
			scrollCls.setSizeWH(_objWidht,_objHeight);
			scrollCls.show(_displayObj);
			
		} 
	}
	
}